package edu.njust.controller;

import edu.njust.Service.Room.RoomService;
import edu.njust.Service.reservation.ReservationService;
import edu.njust.Service.stayregister.StayRegisterService;
import edu.njust.controller.depcontroller.Code;
import edu.njust.controller.depcontroller.Result;
import edu.njust.domain.Reservation;
import edu.njust.domain.StayRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ReservationController")
public class ReservationController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private StayRegisterService stayRegisterService;

    @Autowired
    private RoomService roomService;
    //新增预订
    @PostMapping("/insertAll")
    public Result insertAll(@RequestBody Reservation reservation){
//        System.out.println("here");
        boolean flag = reservationService.insertAll(reservation);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    //分页和模糊查询
    @GetMapping("/pageFuzzyselect/{passengerName}/{predetermineStateID}/{start}/{pageSize}")
    public Result pageFuzzyselect(@PathVariable String passengerName, @PathVariable int predetermineStateID,
                                  @PathVariable int start, @PathVariable int pageSize){
        List<Reservation> reservations = reservationService.pageFuzzyselect(passengerName, predetermineStateID, start, pageSize);
        Integer code = reservations != null ? Code.GET_OK : Code.GET_ERR;
        String msg = reservations != null ? "" : "数据查询失败，请重试！";
        return new Result(code,reservations, msg);
    }
    //查询总条数
    @GetMapping("/countFuzzyselect/{passengerName}/{predetermineStateID}")
    public Result countFuzzyselect(@PathVariable String passengerName, @PathVariable int predetermineStateID){
        int count = reservationService.countFuzzyselect(passengerName, predetermineStateID);
        return new Result(Code.GET_OK,count);
    }

    //id查询
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id){
        Reservation res = reservationService.findById(id);
        Integer code = res != null ? Code.GET_OK : Code.GET_ERR;
        String msg = res != null ? "" : "数据查询失败，请重试！";
        return new Result(code,res, msg);
    }

    //根据 旅客id 来查询
    @GetMapping("/findLvKeId/{id}")
    public Result findLvKeId(@PathVariable int id){
        List<Reservation> reservations = reservationService.findLvKeId(id);
        Integer code = reservations != null ? Code.GET_OK : Code.GET_ERR;
        String msg = reservations != null ? "" : "数据查询失败，请重试！";
        return new Result(code,reservations, msg);
    }
    //id删除
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable int id){
        boolean flag = reservationService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    //查询全部数据 无分页
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Reservation> reservations = reservationService.selectAll();
        Integer code = reservations != null ? Code.GET_OK : Code.GET_ERR;
        String msg = reservations != null ? "" : "数据查询失败，请重试！";
        return new Result(code,reservations, msg);
    }

    @GetMapping("/selectRoomIdIsNull")
    public Result selectRoomIdIsNull(){
        List<Reservation> reservations = reservationService.selectAll();
        List<Reservation> result=new ArrayList<>();
        for(int i=0;i<reservations.size();i++){
            if (reservations.get(i).getRoomID()==null){
                //System.out.println(reservations.get(i).getId());
                result.add(reservations.get(i));
            }
        }
        Integer code = result != null ? Code.GET_OK : Code.GET_ERR;
        String msg = result != null ? "" : "数据查询失败，请重试！";
        return new Result(code,result, msg);
    }

    @GetMapping("/selectRoomIdNotNull")
    public Result selectRoomIdNotNull(){
        List<Reservation> reservations = reservationService.selectAll();
        List<Reservation> result=new ArrayList<>();
        for(int i=0;i<reservations.size();i++){
            if (reservations.get(i).getRoomID()!=null&&reservations.get(i).getPredetermineStateID()==52){
                result.add(reservations.get(i));
            }
        }
        Integer code = result != null ? Code.GET_OK : Code.GET_ERR;
        String msg = result != null ? "" : "数据查询失败，请重试！";
        return new Result(code,result, msg);
    }

    //修改预订到时提示
    @PutMapping("/updateRemind/{id}")
    public Result updateRemind(@PathVariable int id){
        boolean flag = reservationService.updateRemind(id);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @PutMapping("/updatePredetermineStateIDone/{id}")
    public Result updatePredetermineStateIDone(@PathVariable int id){
        boolean flag = reservationService.updatePredetermineStateIDone(id);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @PutMapping("/updatePredetermineStateIDtwo/{id}")
    public Result updatePredetermineStateIDtwo(@PathVariable int id){
        boolean flag = reservationService.updatePredetermineStateIDtwo(id);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @PutMapping("/updateroomID/{id}/{roomID}")
    public Result updateroomID(@PathVariable int id,@PathVariable int roomID){
        boolean flag = reservationService.updateroomID(id,roomID);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    /*
        1.传入id，生成stayRegister对象
        2.对该对象set日期属性
        3.调用stayRegister里面的insert方法
        4.将reservation中预定状态改成
     */
    @PutMapping("/handleStayRegister/{id}/{date}")
    public Result handleStayRegister(@PathVariable int id, @PathVariable Date date){
        StayRegister stayRegister = reservationService.findStayRegister(id);
        stayRegister.setRegisterTime(date);
        stayRegisterService.insertAll(stayRegister);
        reservationService.updatePredetermineStateIDone(id);
        roomService.updateroomStatetwo(reservationService.findById(id).getRoomID());
        Integer code = stayRegister != null ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = stayRegister != null ? "" : "数据查询失败，请重试！";
        return new Result(code,stayRegister,msg);
    }



}
